package laba11;

import interfaces.logging.LoggingArrayCollection;
import interfaces.task5.ArrayIterator;

import java.util.Collection;
import java.util.Iterator;

import laba11.laba5.ArrayCollectionImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Yana
 */
public class LoggingArrayCollectionImpl<E extends Object>
        extends ArrayCollectionImpl
        implements LoggingArrayCollection {

	/**
	 * Create instance of logger
	 */
    private Log logger = LogFactory.getLog(
    		LoggingArrayCollectionImpl.class);

    @Override
    public org.apache.commons.logging.Log getLogger() {
        return logger;
    }

    @Override
    public Object[] getArray() {
        Object[] result = null;
        try {
            result = super.getArray();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public void setArray(Object[] es) {
        try {
            super.setArray(es);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
    }

    @Override
    public int size() {
        int result = 0;
        try {
            result = super.size();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean isEmpty() {
        boolean result = false;
        try {
            result = super.isEmpty();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean contains(Object o) {
        boolean result = false;
        try {
            result = super.contains(o);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public Iterator iterator() {
        Iterator result = null;
        try {
            result = new LoggingArrayIteratorImpl();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    /**
    * Iterator for ArrayCollectionImpl
    */
    private class LoggingArrayIteratorImpl <E extends Object>
    	extends ArrayIteratorImpl implements ArrayIterator {

        @Override
        public Object[] getArray() {
            Object[] result = null;
        try {
            result = super.getArray();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
        }

        @Override
        public boolean hasNext() {
           boolean result = false;
        try {
            result = super.hasNext();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

        @Override
        public Object next() {
           Object result = null;
        try {
            result = super.next();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

        @Override
        public void remove() {
        try {
            super.remove();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
   }
    }

    @Override
    public Object[] toArray() {
        Object[] result = null;
        try {
            result = super.toArray();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public Object[] toArray(Object[] a) {
        Object[] result = null;
        try {
            result = super.toArray(a);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean add(Object e) {
        boolean result = false;
        try {
            result = super.add(e);
            logger.trace(this);
        } catch (Exception ex) {
            logger.error(this, ex);
            throw ex;
        }
        return result;
    }

    @Override
    public boolean remove(Object o) {
        boolean result = false;
        try {
            result = super.remove(o);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean containsAll(Collection c) {
        boolean result = false;
        try {
            result = super.containsAll(c);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean addAll(Collection c) {
        boolean result = false;
        try {
            result = super.addAll(c);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean removeAll(Collection c) {
        boolean result = false;
        try {
            result = super.removeAll(c);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public boolean retainAll(Collection c) {
        boolean result = false;
        try {
            result = super.retainAll(c);
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
        return result;
    }

    @Override
    public void clear() {
        try {
            super.clear();
            logger.trace(this);
        } catch (Exception e) {
            logger.error(this, e);
            throw e;
        }
    }
}
